package com.guosifan.controller;

import com.guosifan.entity.AllVO;
import com.guosifan.entity.Menu;
import com.guosifan.entity.Msg;
import com.guosifan.entity.Type;
import com.guosifan.feign.MenuClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/client/menu")
public class MenuClientController {

    @Autowired
    private MenuClientFeign menuClientFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public AllVO findAll(@RequestParam int page, @RequestParam int limit){
        int count = menuClientFeign.count();
        List<Menu> Menus = menuClientFeign.findAll((page - 1) * limit, limit);
        return new AllVO(0,"",count,Menus);
    }

    @GetMapping("/count")
    @ResponseBody
    public int count(){
        int count = menuClientFeign.count();
        return count;
    }

    @GetMapping("deleteById/{id}")
    public String deleteById(@PathVariable("id") int id){
        menuClientFeign.deleteById(id);
        return "redirect:/menu_manage.html";
    }


    @GetMapping("/findTypes")
    @ResponseBody
    public Msg findTypes(){
        List<Type> types=menuClientFeign.findTypes();
        return Msg.success().add("types", types);
    }

    /**
     * /client/menu/save
     */
    @PostMapping("/save")
    public String save( Menu menu){
        int num = menuClientFeign.save(menu);
        if(num == 1){
            return "redirect:/menu_manage.html";
        }
        return ""; //指向另外一个页面，以区分成功时的页面
    }
}
